package com.geovis.emergency.spd.biz.zwdd.config.schedule;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 定时任务状态切面
 * --仅对handler下的定时任务
 *
 * @author linrf
 * @version V1.0
 * @date 2020/12/5 20:51
 */
@Slf4j
@Aspect
@Component
public class ScheduleAspect {

    @Pointcut("@within(org.springframework.scheduling.annotation.Scheduled) || @annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void schedule() {
    }

    @Pointcut("execution(public * com.geovis.emergency.spd.biz.zwdd.config.schedule.handler.*..*(..))")
    public void handle() {
    }

    /**
     * 开始定时任务
     */
    @Before("schedule() && handle()")
    public void doBefore(JoinPoint joinPoint) {
        Object thiz = joinPoint.getThis();
        if (thiz instanceof AbsScheduleHandler) {
            AbsScheduleHandler scheduleHandler = (AbsScheduleHandler) thiz;
            scheduleHandler.start();
        }
    }

    /**
     * 开始定时任务结束定时任务
     */
    @After("schedule() && handle()")
    public void doAfter(JoinPoint joinPoint) {
        Object thiz = joinPoint.getThis();
        if (thiz instanceof AbsScheduleHandler) {
            AbsScheduleHandler scheduleHandler = (AbsScheduleHandler) thiz;
            scheduleHandler.stop();
        }
    }

}
